import Vue from 'vue'
import upperFirst from 'lodash/upperFirst'
import camelCase from 'lodash/camelCase'
import config from '@govcloud/gsd-web-core-config'
import sdk from '@govcloud/gsd-web-core-sdk'
import ui from '@govcloud/gsd-web-core-ui'
import { request, requestFd } from './sdk/request'
import injectConfig from './config'
import '@/assets/styles/index.scss'

const requireComponent = require.context(
  // 其组件目录的相对路径
  '../components/',
  // 是否查询其子目录
  true,
  // 匹配基础组件文件名的正则表达式
  /_base\.vue$/
)

requireComponent.keys().forEach(fileName => {
  // Get the component config
  const componentConfig = requireComponent(fileName)
  // Get the PascalCase version of the component name
  try {
    const componentName = upperFirst(camelCase(componentConfig.default.name))
    // Globally register the component
    Vue.component(componentName, componentConfig.default || componentConfig)
  } catch (e) {
    console.error(e)
  }
})

ui.init(Vue)
// 注入自定义的sdk，如request模块
sdk.inject({
  request,
  requestFd
})

Vue.prototype.$sdk = sdk
// 注入自己的全局config
config.inject(injectConfig)
Vue.prototype.$config = config

export { Vue }
